var http = require("http");
var url = require("url");
var route = require("./route");

function start(){
	
	http.createServer(function(request, response) {
		if(request.url == "/favicon.ico"){
			return;
		}
//		response.setHeader('Access-Control-Allow-Origin', '*'); // * 这个表示任意域名都可以访问，这样写不能携带cookie了。
//		//response.setHeader('Access-Control-Allow-Credentials', true);  // 允许服务器端发送Cookie数据,
//		response.setHeader('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');//它也是一个逗号分隔的字符串，表明服务器支持的所有头信息字段
//		response.setHeader('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');  //设置方法
//		
	  	var pathname = url.parse(request.url).pathname;
	  	pathname=pathname.replace(/\//,'');
	  	console.log('pathname:'+pathname);
	  	//如果没截到路径名就进默认路由
	  	if(!pathname){
	  		route['index'](request, response);
	  		return;
	  	}
	  	//如果路径名是page开头就进静态路由
	  	if(pathname.indexOf("page")>=0){
	  		route['gotoPage'](pathname,request, response);
	  		return;
	  	}
	  	//接口路由
	  	try{
	  		route[pathname](request, response);
	  	}catch(e){
	  		console.log(e);
	  		route['error'](request, response);
	  	}
	}).listen(8888);
	
}

exports.start=start;